package com.slg.android.manyanime.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.adapter.AnimeFragmentAdapter;
import com.slg.android.manyanime.callBackInterface.AnimeCalltoMain;

public class AnimeFragment extends Fragment implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {
    @ViewInject(R.id.anime_navigation_radioGroup)
    private RadioGroup navigationBar;
    @ViewInject(R.id.anime_function_viewPager)
    private ViewPager viewPager;
    @ViewInject(R.id.anime_sliding_imageView)
    private ImageView toSliding;

    private AnimeCalltoMain animeCalltoMain;


    public void setAnimeCalltoMain(AnimeCalltoMain animeCalltoMain) {
        this.animeCalltoMain = animeCalltoMain;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_anime, container, false);
        ViewUtils.inject(this, view);
        navigationBar.setOnCheckedChangeListener(this);
        viewPager.setAdapter(new AnimeFragmentAdapter(getActivity().getSupportFragmentManager()));
        viewPager.setOnPageChangeListener(this);
        Log.i("AnimeFragment", "onCreateView");
        toSliding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animeCalltoMain.dosomething();
            }
        });
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i("AnimeFragment", "onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewPager.removeAllViews();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for (int i = 0; i < group.getChildCount(); i++) {
            RadioButton child = (RadioButton) group.getChildAt(i);
            if (child.isChecked()) {
                //Log.i("checked","pos"+i);
                child.setTextColor(Color.WHITE);
                child.setBackgroundDrawable(
                        getResources().getDrawable(R.drawable.navigation_bar_item_checked_bg));
                viewPager.setCurrentItem(i);
            } else {
                child.setTextColor(getResources().getColor(R.color.main_menu_text));
                child.setBackgroundDrawable(
                        getResources().getDrawable(R.drawable.navigation_bar_item_bg));
            }

        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Log.i("selectedPage", "pos" + position);
        if (navigationBar != null) {
            RadioButton child = (RadioButton) navigationBar.getChildAt(position);
            child.setChecked(true);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
